/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import persistencia.AtiendeDAO;
import persistencia.EmpleadosDAO;
import persistencia.PacientesDAO;
import persistencia.ProcesoasistencialDAO;
import persistencia.UrgenciasDAO;
import pojos.Atiende;
import pojos.AtiendeId;
import pojos.Empleados;
import pojos.Paciente;
import pojos.Procesoasistencial;
import pojos.Urgencias;

/**
 *
 * @author Administrator
 */
public class JDialogAddUrgencias extends javax.swing.JDialog {

    
     UrgenciasDAO dao=new UrgenciasDAO();
     
    public JDialogAddUrgencias(JPanel panelPadre) {
        initComponents();
        this.setModal(true);
        this.setLocationRelativeTo(panelPadre);
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        dcFecha = new datechooser.beans.DateChooserCombo();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tftEmpleado = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Fecha Urgencia:");

        jButton1.setText("Añadir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("ID Paciente");

        jLabel4.setText("ID Empleado:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tftEmpleado)
                        .addGap(163, 163, 163))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton1)
                            .addComponent(dcFecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextField1))
                        .addGap(109, 109, 109))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tftEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dcFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(32, 32, 32)
                .addComponent(jButton1)
                .addContainerGap(122, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
         if (dcFecha.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Debes rellenar todos los datos", "NO HAY DATOS", JOptionPane.INFORMATION_MESSAGE);
        } else {
            try {
                Empleados empleado = new EmpleadosDAO().buscaEmpleadosCodigo(Integer.parseInt(this.tftEmpleado.getText())).get(0);
                Paciente paciente = new PacientesDAO().getPaciente(Integer.parseInt(this.jTextField1.getText()));
                if (paciente == null) {
                    JDialogAgregarPaciente ap = new JDialogAgregarPaciente();
                    ap.setVisible(true);
                } else {
                    Procesoasistencial pa = new Procesoasistencial("Urgencias");
                    Procesoasistencial lastPA = new ProcesoasistencialDAO().guardaProcesoasistencial(pa);
                    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
                    Date fecha = formatter.parse(dcFecha.getText());
                    dao.addUrgencia(new Urgencias(lastPA, fecha,null, null));
                    AtiendeId atiendeID = new AtiendeId(empleado.getCodEmpleado(), paciente.getCodPaciente(), pa.getIdPa());
                    Atiende atiende = new Atiende(atiendeID, empleado, paciente, lastPA);
                    new AtiendeDAO().guardaAtiende(atiende);
                    
                    jTextField1.setText("");
                }
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "Debes introducir datos numericos en el campo de texto", "ERROR DE DATOS", JOptionPane.WARNING_MESSAGE);
            } catch (ParseException ex) {
                JOptionPane.showMessageDialog(this, "Error al convertir cadena a fecha", "ERROR DE CONVERSION", JOptionPane.WARNING_MESSAGE);
            } catch (IndexOutOfBoundsException ex) {
                JOptionPane.showMessageDialog(this, "Debes introducir un empleado existente", "SIN EMPLEADO", JOptionPane.WARNING_MESSAGE);
            
        }
    }//GEN-LAST:event_jButton1ActionPerformed
}
    /**
     * @param args the command line arguments
     */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private datechooser.beans.DateChooserCombo dcFecha;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField tftEmpleado;
    // End of variables declaration//GEN-END:variables
}
